using System.Collections;
using UnityEngine;

public class LobbyPlayerGunManager : MonoBehaviour
{
	public static LobbyPlayerGunManager instance;

	public Transform weaponHolder;

	public Transform weaponHolderLeft;

	private GameObject _instantiatedWeapon;

	private GameObject _instantiatedWeaponLeft;

	[Space(10f)]
	[SerializeField]
	private PlayerMecanimWrapper mecanimWrapper;

	private WeaponMecanimWrapper weaponMecanimWrapper;

	[SerializeField]
	private EquipmentManager equipmentManager;

	public GameObject lightObj;

	public bool changeWeaponLayer;

	private BaseWeapon currentWeaponScript;

	private IEnumerator _playAnim;

	private static string newWeaopn = string.Empty;

	private void Awake()
	{
		if (lightObj != null)
		{
			instance = this;
		}
		mecanimWrapper = GetComponent<PlayerMecanimWrapper>();
		equipmentManager = GetComponent<EquipmentManager>();
	}

	private void Start()
	{
		Refresh();
		if (_playAnim == null)
		{
			_playAnim = PlayAnimation();
			StartCoroutine(_playAnim);
		}
		MarketController.Instance.OnHideMarket += Refresh;
		GotchaManagerUI.OnHideGotcha += Refresh;
	}

	private void OnDestroy()
	{
		instance = null;
		MarketController.Instance.OnHideMarket -= Refresh;
		GotchaManagerUI.OnHideGotcha -= Refresh;
	}

	private void Refresh()
	{
		SetSkin();
		equipmentManager.PutOnAllItems();
		if (newWeaopn == string.Empty)
		{
			InstantiateWeapon(MarketController.Instance.GetRandomEquipedWeapon());
			return;
		}
		InstantiateWeapon(newWeaopn);
		newWeaopn = string.Empty;
	}

	public void InstantiateWeapon(string key)
	{
		GameObject gameObject = Resources.Load("Weapons/" + key) as GameObject;
		if (!(gameObject != null))
		{
			return;
		}
		Object.Destroy(_instantiatedWeapon);
		if (_instantiatedWeaponLeft != null)
		{
			Object.Destroy(_instantiatedWeaponLeft);
		}
		_instantiatedWeapon = (GameObject)Object.Instantiate(gameObject, weaponHolder.position, weaponHolder.rotation);
		if (changeWeaponLayer)
		{
			Renderer[] componentsInChildren = _instantiatedWeapon.GetComponentsInChildren<Renderer>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				componentsInChildren[i].gameObject.layer = 26;
			}
		}
		_instantiatedWeapon.transform.parent = weaponHolder;
		currentWeaponScript = _instantiatedWeapon.GetComponent<BaseWeapon>();
		currentWeaponScript.isMobWeapon = true;
		currentWeaponScript.chargerAmmo = currentWeaponScript.maxChargerAmmo;
		currentWeaponScript.Init(1, weaponHolder, weaponHolderLeft, GetComponentInChildren<Collider>());
		weaponMecanimWrapper = currentWeaponScript.weaponMecanimWrapper;
		mecanimWrapper.SetWeapon(weaponMecanimWrapper.overrideController, currentWeaponScript.reloadTime, currentWeaponScript.timeBetweenShots, currentWeaponScript.weaponMecanimWrapper.pullable);
		weaponMecanimWrapper.Init(mecanimWrapper, currentWeaponScript.reloadTime, currentWeaponScript.timeBetweenShots, true);
		ParticleSystem[] componentsInChildren2 = _instantiatedWeapon.GetComponentsInChildren<ParticleSystem>(true);
		ParticleSystem[] array = componentsInChildren2;
		foreach (ParticleSystem particleSystem in array)
		{
			particleSystem.gameObject.SetActive(false);
		}
	}

	public void SetSkin()
	{
		Renderer[] componentsInChildren = GetComponentsInChildren<Renderer>();
		if (componentsInChildren.Length == 0)
		{
			return;
		}
		Renderer[] array = componentsInChildren;
		foreach (Renderer renderer in array)
		{
			if (renderer.gameObject.tag != "Weapon")
			{
				renderer.material.mainTexture = MarketController.Instance.GetEquipedSkinTexture();
			}
		}
	}

	private IEnumerator PlayAnimation()
	{
		while (true)
		{
			yield return new WaitForSeconds(15f);
			weaponMecanimWrapper.PlayDemoAnim();
			mecanimWrapper.PlayDemoAnim();
		}
	}

	public static void ShowNewWeapon(string nameWeapon)
	{
		newWeaopn = nameWeapon;
	}
}
